package com.java.controller;

import com.github.pagehelper.PageInfo;
import com.java.model.*;
import com.java.service.AdminService;
import com.java.service.LaunchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//登录管理员后台
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private LaunchService launchService;

    @RequestMapping("/list")
    @ResponseBody
    public Result list(Admin admin) {
        return adminService.list(admin);
    }

    @RequestMapping("/toList")
    public String toList() {
        return "admin/list";
    }

    @RequestMapping("/openLogin")
    public String openLogin() {
        return "user/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request,String phone, String password, Model model){
        Admin admin = new Admin();
        admin.setPhone(phone);
        admin.setPassword(password);
        return adminService.login(admin,request,model);
    }

    @RequestMapping("/index")
    public String adminIndex(){
        return "/admin/index";
    }

    /*管理员投放管理页面*/
    @RequestMapping("/openList")
    public String adminList(HttpServletRequest request, Model model){
//        User user =(User) request.getSession().getAttribute("user");
//        model.addAttribute("id",user.getId());
        List<Status> state = launchService.state();
        model.addAttribute("state",state);
        return "/admin/launch/list";
    }



    @RequestMapping("/remove")
    @ResponseBody
    public Result remove(int[] ids) {
        return adminService.remove(ids);
    }


    @RequestMapping("/add")
    public String add() {
        return "/admin/add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result save(@RequestBody Admin admin) {
        return adminService.save(admin);
    }

    @GetMapping("/edit")
    public String edit(Model model, int id) {
        Result result = adminService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("admin", result.getData());
        }

        return "/admin/edit";
    }

    @PostMapping("/modify")
    @ResponseBody
    public Result modify(@RequestBody Admin admin) {
        return adminService.modify(admin);
    }



    @RequestMapping("/enter")
    public String enter(){
        return "/admin/enter/list";
    }

    //轮播图
    @RequestMapping("/slideshow")
    public String slideshow(){
        return "/admin/slideshow";
    }
}
